<?php

/**
 * Short Description  
 *
 * Long Description 
 *
 * @copyright 
 * @version   $Id$
 */

require_once 'Core/DAO.php';

class SettingsDAO extends Core_DAO
{
	protected $_name        = 'settings';
	protected $_primary 	= 'setting_name';
	
	/**
	 * Add new setting if setting with same name exists it will update
	 * @param string $name  - Name of the setting
	 * @param string $value - value of the setting
	 */
	public function addSetting($name, $value)
	{
		if ($this->getSetting($name) == null) {
			return $this->insert(array("setting_name" => $name, "value" => $value));
		} else {
			$where = $this->getDefaultAdapter()
                      ->quoteInto('setting_name = ?', $name);
		
			$data   = array('value' => $value);
			$result = $this->updateData($data, $where);	
			
			return $result;
		}
	}
	
	/**
	 * Get setting of given name, generally 
	 * we are not using this for getting setting 
	 * in application, it will used to check
	 * if the setting with same name already exists
	 * 
	 * @param unknown_type $name
	 */
	public function getSetting($name)
	{
		return $this->select()
					->from($this->_name)
					->where("setting_name = ?", $name)
					->query()
					->fetch();
	}
	
	/**
	 * Get all settings available in db
	 */
	public function getAllSettings()
	{
		return $this->select()
					->from($this->_name)
					->query()
					->fetchAll();
	}
	
	/**
	 * @todo Need to do delete of setting, 
	 * really need it?
	 */
}